package com.example.rabbitmqprovider.direct.controller;

import cn.binarywang.tools.generator.ChineseAddressGenerator;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.hutool.core.util.StrUtil;
import com.example.rabbitmqprovider.direct.config.DirectRabbitConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
public class SendCallbackMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @ResponseBody
    @GetMapping("/sendMessageToExchangeFail")
    public Object sendMessageToExchangeFail() {
        String messageData = StrUtil.format("hello 我叫：{} 我住在：{}", ChineseNameGenerator.getInstance().generate(),
                ChineseAddressGenerator.getInstance()
                        .generate());
        Map<String,Object> map = new HashMap<>();
        map.put("messageData",messageData);
        //发送一个消息到 不存在到exchange
        rabbitTemplate.convertAndSend(DirectRabbitConfig.TestDirectExchange.concat("test"), DirectRabbitConfig.TestDirectRouting, map,new CorrelationData(UUID.randomUUID().toString()));
        return map;
    }
    @ResponseBody
    @GetMapping("/sendMessageToQueueFail")
    public Object sendMessageToQueueFail() {
        String messageData = StrUtil.format("hello 我叫：{} 我住在：{}", ChineseNameGenerator.getInstance().generate(),
                ChineseAddressGenerator.getInstance()
                        .generate());
        Map<String,Object> map = new HashMap<>();
        map.put("messageData",messageData);
        //发送一个消息到 不存的队列里；
        rabbitTemplate.convertAndSend(DirectRabbitConfig.TestDirectExchange, "xxx", map,new CorrelationData(UUID.randomUUID().toString()));
        return map;
    }
}